package com.ts.manage.cms.controller;

import com.ts.api.manage.cms.CmsPreviewPostControllerApi;
import com.ts.framework.domain.cms.CmsPage;
import com.ts.framework.domain.cms.response.CmsPageResult;
import com.ts.framework.domain.cms.response.CmsPostPageResult;
import com.ts.framework.model.response.ResponseResult;
import com.ts.framework.web.BaseController;
import com.ts.manage.cms.service.CmsPreviewPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/cms")
public class CmsPreviewPostController extends BaseController implements CmsPreviewPostControllerApi {

    @Autowired
    CmsPreviewPostService cmsPreviewPostService;

    /**
     * 页面预览
     * @param pageId
     */
    @Override
    @GetMapping("/preview/{pageId}")
    public void previewCmsPage(@PathVariable("pageId") String pageId) {
        try {
            String content = cmsPreviewPostService.getHtmlPage(pageId);
            response.setHeader("Content-type","text/html;charset=utf-8");
            response.getWriter().write(content);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 页面发布
     * @param pageId
     * @return
     */
    @Override
    @PostMapping("/page/postPage/{pageId}")
    public ResponseResult PostCmsPage(@PathVariable("pageId") String pageId) {
        return cmsPreviewPostService.postPage(pageId);
    }

    /**
     * 添加修改页面信息
     * @param cmsPage
     * @return
     */
    @Override
    @PostMapping("/page/save")
    public CmsPageResult saveCmsPage(@RequestBody CmsPage cmsPage) {
        return cmsPreviewPostService.saveCmsPage(cmsPage);
    }

    /**
     * 一键发布页面
     * @param cmsPage
     * @return
     */
    @Override
    @PostMapping("/page/postPageQuick")
    public CmsPostPageResult postPageQuick(@RequestBody CmsPage cmsPage) {
        return cmsPreviewPostService.postPageQuick(cmsPage);
    }


}
